#include"contact.h"
 
//初始化通讯录和申请空间
void Init(Contact* pc)
{
	
	pc->sz = 0;
	Information *tmp = (Information*)malloc( 3*sizeof(Information));//用来申请内存条
	
	if (tmp != NULL)
	{
		pc->data = tmp;
	}
	else
	{
		printf("初始化失败\n");
		return;
	}
	
	pc->capacity = 3 ;
	Load(pc);
	
 
}
//判断是否增容
int  Addcapacity(Contact* pc)
{
	
	if (pc->sz == pc->capacity)
	{
		Information* tmp = (Information*)realloc(pc->data, sizeof(Information)*(pc->capacity+2));
		if (tmp != NULL)
		{
			pc->data = tmp;
			pc->capacity += 2;
			printf("增容成功\n");
			return 1;
		}
		else
		{
			
			printf("增容失败\n");
			return 0;
		}
	}
	return 1;
	
}

//加载文件
void Load(Contact* pc)
{
	
	//打开文件
	FILE* pf = fopen("contact.txt", "rb");
	if (pf == NULL)
	{
		printf("错误\n");
		return;
	}
	//读文件
	Information tmp = { 0 };
	while (fread(&tmp, sizeof(Information), 1, pf))
	{
		Addcapacity(pc);
		pc->data[pc->sz] = tmp;
		pc->sz++;
	}
	fclose(pf);
	pf = NULL;
}
//联系人
void Add(Contact* pc)
{	
    
	int i=Addcapacity(pc);
	if (i)
	{
		
		printf("请输入姓名->");
		scanf("%s", pc->data[pc->sz].name);
		printf("请输入职称->");
		scanf("%s", pc->data[pc->sz].title);
		printf("请输入电话->");
		scanf("%d", &(pc->data[pc->sz].tele));
		printf("请输入职位->");
		scanf("%s", pc->data[pc->sz].post);
		printf("请输入电子邮箱->");
		scanf("%s", pc->data[pc->sz].email);
		 
		pc->sz++;
		printf("添加联系人成功！");
	}
	else
	{
		printf("添加联系人失败\n");
	}
	
	
	
 
	
}
void Show(Contact* pc)//显示通讯录
{
	
	if (pc->sz == 0)
	{
		printf("通讯录里无联系人！\n");
	}
	else
	{
		int i = 0;
		printf("%-20s%-20s%-20s%-20s%-20s\n", "姓名", "职位", "电话", "职称", "电子邮箱");
		for (i = 0; i < pc->sz; i++)
		{
			printf("%-20s%-20s%-20d%-20s%-20s\n",
				pc->data[i].name, 
				pc->data[i].title, 
				pc->data[i].tele,
				pc->data[i].post, 
				pc->data[i].email);
		}
	}
}
int Find_Name(char* name, Contact* pc)//查找联系人，返回其下标
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (*name == *(pc->data[i].name))
		{
			return i;
		}
	}
	return -1;
}
 
void Delete(Contact* pc)//删除联系人
{
	char name[20] = {0};
	int i = 0;
	printf("请输入要删除的联系人姓名->");
	scanf("%s", name);
	//查找联系人
	int flag=Find_Name(name,pc);
	if (flag != -1)
	{
		for (i = flag; i < pc->sz-1; i++)
		{
			pc->data[i] = pc->data[i + 1];
		}
		pc->sz--;
		printf("删除成功！\n");
	}
	else
	{
		printf("没有找到您需要删除的人！\n");
	}
}
void Search(Contact* pc)//查找联系人
{
	char name[20] = {0};
	printf("请输入要查找的联系人姓名->");
	scanf("%s", name);
	int flag = Find_Name(name, pc);
	if (flag != -1)
	{
		printf("%-20s%-20s%-20s%-20s%-20s\n", "姓名", "职位", "电话", "职称", "电子邮箱");
 
		printf("%-20s%-20s%-20d%-20s%-20s\n",
			pc->data[flag].name, 
			pc->data[flag].post, 
			pc->data[flag].tele,
			pc->data[flag].title, 
			pc->data[flag].email);
	}
	else
	{
		printf("查无此人！\n");
	}
}
 
void Modify(Contact* pc)//修改联系人
{
	char name[20] = {0};
	printf("请输入要修改的联系人姓名->");
	scanf("%s", name);
	int flag = Find_Name(name, pc);
	if (flag == -1)
	{
		printf("查无此人\n");
		return;
	}
	else
	{
		int input = 0;
		do
		{
			
			printf("1.姓名  2.职位  3.电话  4.职称  5.电子邮箱  0.退出\n");
			printf("请输入要修改的选项(按0退出修改)->");
			scanf("%d", &input);
			switch (input)
			{
				case 1:
				
					printf("请输入改正后的姓名->");
					scanf("%s", pc->data[flag].name);
					break;
				case 2:
					printf("请输入改正后的职位->");
					scanf("%d", pc->data[flag].post);
					break;
				case 3:
					printf("请输入改正后的电话->");
					scanf("%s", (&pc->data[flag].tele));
					break;
				case 4:                                                                                                                                                                                                                                                  
					printf("请输入改正后的职称->");
					scanf("%s", pc->data[flag].title);
					break;
				case 5:
					printf("请输入改正后的电子邮箱->");
					scanf("%s", pc->data[flag].email);
					break;
				case 0:
					break;
				default:
					printf("输入错误，请重新输入！\n");
					break;
			}
		} while(input);
		printf("修改成功！\n");
	}
 
}
 

//对联系人进行排序

void Sort(Contact* pc)
{
	int i = 0;
	for(i = 0;i < pc->sz - 1;i++)
	{
	int flag = 0;
	int j;
	for(j = 0;j < pc->sz-1-i;j++)
    {
	if(strcmp(pc->data[j].name,pc->data[j+1].name) > 0)
	{
	Information tmp = pc->data[j];
	pc->data[j] = pc->data[j+1];
	pc->data[j+1] = tmp;
	flag++;
	
	}
	
	}
	if(flag == 0)
	{
	return;
	}
	}
    
	}
	
	
	
	}


void Destroy(Contact* pc)//空间销毁
{
	free(pc->data);
	pc->data = NULL;
	pc->capacity = 0;
	pc->sz = 0;
}


//保存文件
void Save(Contact* pc)
{
	FILE* fp = fopen("contact.txt", "wb");
	if (fp == NULL)
	{
		
		printf("错误\n");
		return;
	}
	//写入文件
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		fwrite(pc->data + i, sizeof(Information), 1, fp);
	}
	//关闭文件
	fclose(fp);
	 fp = NULL;
}